package com.meihua.shellapp.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.CashBean;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.databean.BankDataBean;
import com.meihua.shellapp.utils.URL;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 我的钱包 提现界面
 */
public class WithdrawalsActivity extends BaseActivity {
    private final int CODE_STARTBANK = 001;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.withdrawal_rl_choose_bank)
    RelativeLayout mChoose_bank;
    @Bind(R.id.withdrawal_et_money)
    EditText mEtmoney;
    @Bind(R.id.withdrawal_tv_bank)
    TextView mTvBank;

    private BankDataBean bankBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_withdrawals;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("提现");
        mIVTopLeft.setVisibility(View.VISIBLE);
        loadData();
    }

    /*加载服务器数据*/
    private void loadData() {
        Map<String, String> params = new HashMap() {{
            put("m_number", MyApplication.getInstance().getUserId());
        }};
        NetHelper.post(URL.getUrl().CASH, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                CashBean bean = MyApplication.getInstance().getBeanForJson(CashBean.class, response.body());
                if (bean == null) return;
                CashBean.DataBean data = bean.getData();
//                bank_name = data.getBank_name();
//                mTvBank.setText(bank_name);
                mEtmoney.setHint("可提现金额为" + data.getCash_income() + "元");
//                bank_card = data.getBank_card();
            }
        }, true);
    }


    @OnClick({R.id.iv_top_left, R.id.withdrawal_btn_next, R.id.withdrawal_rl_choose_bank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_top_left:
                finish();
                break;
            case R.id.withdrawal_rl_choose_bank:
                /*跳到银行卡列表界面*/
                startActivityForResult(new Intent(this, BankcardActivity.class).putExtra("type", 2), CODE_STARTBANK);
                break;
            case R.id.withdrawal_btn_next:
                /*申请提现界面*/
                if (TextUtils.isEmpty(mTvBank.getText().toString()) || bankBean == null) {
                    Toast.makeText(this, "请输入银行卡信息", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mEtmoney.getText().toString().trim())) {
                    Toast.makeText(this, "请输入提现金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(this, ApplyWithdrawwalsActivity.class);
                intent.putExtra("bank_card", bankBean.getBanks_card());
                intent.putExtra("bank_name", bankBean.getBanks_name());
                intent.putExtra("money", mEtmoney.getText().toString().trim());
                startActivityForResult(intent, 01);
                break;
        }
    }

    /*界面返回的数据*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 11) finish();
        if (requestCode == CODE_STARTBANK && data != null) {
            bankBean = (BankDataBean) data.getSerializableExtra("bankCard");
            if (bankBean == null) {
                return;
            } else {
                mTvBank.setText(bankBean.getBanks_name());
            }
        }
    }
}
